package com.olinsports.system.olinsportssystem.service;

import com.olinsports.system.olinsportssystem.model.MemberInformation;
import com.olinsports.system.olinsportssystem.model.SavingDetail;
import com.olinsports.system.olinsportssystem.model.StmUsr;
import com.olinsports.system.olinsportssystem.repository.MemberInformationRepository;
import com.olinsports.system.olinsportssystem.util.MemberInformationExcel;
import com.olinsports.system.olinsportssystem.util.PageUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service
public class MemberInformationService {
    @Autowired
    private MemberInformationRepository memberInformationRepository;
    @Autowired
    private SavingDetailService savingDetailService;

   
    private int totalPages = 0;//总页数

    private int  totalRows;//总数据量
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;


    public MemberInformation selectByMemberKey(int memberKey){
        return memberInformationRepository.selectByMemberKey(memberKey);
    }

    public Map listMembefInfo(String page, String rows,String sord, String sidx){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
            //查询当前表中的总数据量
            totalRows = memberInformationRepository.findCount();
            System.out.println("totalRows = "+totalRows);
            //通过分页进行查询当前表中的数据
            List<MemberInformation> stmCountList =  memberInformationRepository.findLimit(sord,sidx,beg,Integer.valueOf(rows));

            Map mapList = pageUtil.paging(rows,page,totalRows);
            mapList.put("rows",stmCountList);//设置行显示的数据

            return mapList;
    }

    public Map searchDate(String page, String rows,String startDate,String endDate){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = memberInformationRepository.selectByDateCount(startDate,endDate);
        List<MemberInformation> memberInformations =  memberInformationRepository.selectByDateLimit(startDate,endDate,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",memberInformations);//设置行显示的数据
        return mapList;
    }


    public ModelAndView toDcExcel(String startDate, String endDate, HSSFWorkbook workbook) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        List<MemberInformation> savingDetails = memberInformationRepository.selectByDateAll(startDate,endDate);
        map.put("inventoryDetailss",savingDetails);
        MemberInformationExcel memberInformationExcel = new MemberInformationExcel("会员信息","会员信息");
        memberInformationExcel.buildExcelDocument(map, workbook, request, response);
        return new ModelAndView(memberInformationExcel, map);
    }

    public Map searchSelf(String page, String rows,String fieldValue){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = memberInformationRepository.selectSelfCount(fieldValue);
        List<MemberInformation> memberInformations =  memberInformationRepository.selectSelfLimit(fieldValue,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",memberInformations);//设置行显示的数据
        return mapList;
    }

    public int saveOrUpdate(MemberInformation memberInformation){
        Date date = new Date(System.currentTimeMillis());//获取当前时间


        StmUsr stmUsr = (StmUsr)request.getSession().getAttribute("stmUsr");

        MemberInformation memberInformation1 = memberInformationRepository.selectByMemberKey(memberInformation.getMemberKey());

        if(memberInformation1 == null){//添加操作
            memberInformation.setRegDate(date);

            SavingDetail savingDetail = new SavingDetail();
            savingDetail.setDate(date);
            savingDetail.setType("会员办卡");
            savingDetail.setVipNum(memberInformation.getVipNum());
            savingDetail.setVipName(memberInformation.getMenberName());
            savingDetail.setAmount(memberInformation.getBalance());
            savingDetail.setOperatorNum(memberInformation.getOperatorNum());
            savingDetail.setActivityAmount(memberInformation.getActivityAmount());
            savingDetail.setDepositAmount(memberInformation.getAmount());
            int s = savingDetailService.insertAll(savingDetail);
            int m = memberInformationRepository.insertAll(memberInformation);
            if(s > 0 && m>0){
                return 1;
            } else {
                return 0;
            }
        } else {
            memberInformation.setAmdLog(date);
            memberInformation.setAmdUsr(stmUsr);
            return memberInformationRepository.updateData(memberInformation);

        }

    }

    public int getSumTotal(){
        List<MemberInformation> memberInformations = memberInformationRepository.selectAll();
        int sumTotal = 0;
        for(MemberInformation memberInformation : memberInformations){
            sumTotal += memberInformation.getCostSum();
        }
        return sumTotal;
    }

    public MemberInformation selectByVipNum(String vipNum){
        return memberInformationRepository.selectByVipNum(vipNum);
    }

    public int updateMmberSaving(MemberInformation memberInformation,String comment){
        //savingDetail.setType("会员存款");
        Date date = new Date(System.currentTimeMillis());//获取当前时间
        StmUsr stmUsr = (StmUsr)request.getSession().getAttribute("stmUsr");

        SavingDetail savingDetail = new SavingDetail();
        savingDetail.setType("会员存款");
        savingDetail.setDate(date);
        savingDetail.setVipName(memberInformation.getMenberName());
        savingDetail.setVipNum(memberInformation.getVipNum());
        savingDetail.setAmount(memberInformation.getBalance());
        savingDetail.setComment(comment);
        savingDetail.setActivityAmount(memberInformation.getActivityAmount());
        savingDetail.setDepositAmount(memberInformation.getBalance()-memberInformation.getActivityAmount());

        memberInformation.setAmount(0);
        memberInformation.setAmdUsr(stmUsr);
        memberInformation.setAmdLog(date);
        int m =  memberInformationRepository.updateData(memberInformation);
        if(m > 0){
            int s = savingDetailService.insertAll(savingDetail);
            if(s>0){
                return 1;
            } else {
                return 0;
            }
        } else{
            return 0;
        }
    }
}
